package router

import (
	"fmt"
	"service/components"
	"service/modules"
	"service/utils"
	"strings"

	"github.com/gin-gonic/gin"
)

func DepartmentRouter(router *gin.RouterGroup) {
	router.POST("", func(context *gin.Context) {
		d := modules.Department{}
		err := context.ShouldBind(&d)
		if err != nil {
			utils.HandleFailResponse(context, "数据错误!", err.Error())
			return
		}
		fmt.Println("=> ", d)
		_, err = modules.FetchDepartmentByNameService(d.Name)
		if err == nil {
			utils.HandleSuccessResponse(context, d.Name+" 部门已存在，不需要创建！", d)
			return
		}
		err = modules.CreateDepartmentService(d)
		if err != nil {
			utils.HandleFailResponse(context, "创建部门失败!", err.Error())
			return
		}
		utils.HandleSuccessResponse(context, "创建部门成功!", d.Name)
	})
	router.GET("", func(context *gin.Context) {
		name := context.Query("name")
		if name != "" {
			d, err := modules.FetchDepartmentByNameService(name)
			if err != nil {
				utils.HandleFailResponse(context, "获取部门失败!", err.Error())
				return
			}
			utils.HandleSuccessResponse(context, "获取部门成功!", d)
			return
		}
		ds, err := modules.FetchDepartmentService()
		if err != nil {
			utils.HandleFailResponse(context, "获取部门失败!", err.Error())
			return
		}
		utils.HandleSuccessResponse(context, "获取部门成功!", ds)
	})
	router.PATCH("", func(context *gin.Context) {
		d := &modules.Department{}
		err := context.ShouldBind(&d)
		if err != nil {
			utils.HandleFailResponse(context, "数据错误!", err.Error())
			return
		}
		od, err := modules.FetchDepartmentByIDService(*d)
		if err != nil {
			utils.HandleFailResponse(context, "获取部门失败!", err.Error())
			return
		}
		if od.Name != d.Name {
			d, err := modules.FetchDepartmentByNameService(d.Name)
			if err == nil {
				utils.HandleFailResponse(context, "该部门已存在！！!", nil)
				return
			}
			components.DB.Model(&modules.Users{}).
				Where("department = ?", od.Name).
				Update("department", d.Name)
		}
		err = modules.PatchDepartmentService(d)
		components.DB.Model(&modules.Users{}).
			Where("department = ?", d.Name).
			Updates(map[string]interface{}{
				"permissions":  d.Permissions,
				"edit_account": true,
			})
		if err != nil {
			utils.HandleFailResponse(context, "更新部门失败!", err.Error())
			return
		}
		utils.HandleSuccessResponse(context, "更新部门成功!", d)
	})
	router.DELETE("", func(ctx *gin.Context) {
		var depart modules.Department
		ctx.ShouldBind(&depart)
		us := []modules.Users{}
		db := components.DB.Model(&modules.Users{}).
			Where("department = ?", depart.Name)
		db.Find(&us)
		for _, u := range us {
			up := strings.Split(u.Permissions, ",")
			dp := strings.Split(depart.Permissions, ",")
			newDepart := []string{}
			for _, u1 := range up {
				// dp -> u 不要它
				if u1 == "1" || u1 == "2" || u1 == "3" || u1 == "4" {
					newDepart = append(newDepart, u1)
					continue
				}
				if !utils.FindStringInAarrayByValue(dp, u1) {
					newDepart = append(newDepart, u1)
				}

			}
			fmt.Println(u.UserName, " => range 新的权限 ", strings.Join(newDepart, ","))
			db.Where("user_name = ?", u.UserName).Select("permissions").Update("permissions", strings.Join(newDepart, ","))
			db.Select("department").Update("department", "")

		}
		components.DB.Model(&modules.Department{}).
			Where("id = ?", depart.ID).Delete(&modules.Department{})
		utils.HandleSuccessResponse(ctx, "删除部门成功", nil)
	})
}
